package org.ilanchong.droidcontroller;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RemoteControlActivity extends Activity  implements OnClickListener, OnTouchListener {
	private Vibrator mVibrator;
	private ImageView mTouchView;
	private TextView mMessageTextView;
	private Button mPlayPauseButton;
	
	private String mRemoteIp;
	private int mRemotePort;
	private String mRemoteHostname;
	private ComputerInfo mRemoteComputer;
	
	private boolean isPlay = false;
		
	private final int[] mControlButtons = {
		R.id.button_player_next,
		R.id.button_player_play_pause,
		R.id.button_player_pre
	};
	
	private GestureDetector mGestureDetector;
	private static final int FLING_MIN_DISTANCE = 50;
    private static final int FLING_MIN_VELOCITY = 100;
    
    private static final int TOUCH_FOCUS_UP = 1001;
    private static final int TOUCH_FOCUS_DOWN = 1002;
    private static final int TOUCH_FOCUS_LEFT = 1003;
    private static final int TOUCH_FOCUS_RIGHT = 1004;
    private static final int REMOTE_PLAYER_NEXT = 2001;
    private static final int REMOTE_PLAYER_PRE = 2002;
    private static final int REMOTE_PLAYER_PAUSE = 2003;
    private static final int REMOTE_PLAYER_PLAY = 2004;
    
    private Handler mHandler = new Handler(){
    	@Override 
    	public void handleMessage(Message msg) { 
    		switch(msg.what){
    		case TOUCH_FOCUS_UP:
    			mMessageTextView.setText("向上");
    			//DroidController.sendCommond(mRemoteComputer, mRemotePort, )
    			break;
    		case TOUCH_FOCUS_DOWN:
    			mMessageTextView.setText("向下");
    			break;
    		case TOUCH_FOCUS_LEFT:
    			mMessageTextView.setText("向左");
    			break;
    		case TOUCH_FOCUS_RIGHT:
    			mMessageTextView.setText("向右");
    			break;
    		case REMOTE_PLAYER_NEXT:
    			mMessageTextView.setText("下一个");
    			DroidController.sendCommond(mRemoteComputer, mRemotePort, ControlCommand.CMD_CONTROL_NEXT);
    			break;
    		case REMOTE_PLAYER_PRE:
    			mMessageTextView.setText("前一个");
    			DroidController.sendCommond(mRemoteComputer, mRemotePort, ControlCommand.CMD_CONTROL_PRE);
    			break;
    		case REMOTE_PLAYER_PLAY:
    			mMessageTextView.setText("播放");
    			DroidController.sendCommond(mRemoteComputer, mRemotePort, ControlCommand.CMD_CONTROL_PLAY);
    			mPlayPauseButton.setBackgroundResource(R.drawable.remote_player_control_pause);
    			break;
    		case REMOTE_PLAYER_PAUSE:
    			mMessageTextView.setText("暂停");
    			DroidController.sendCommond(mRemoteComputer, mRemotePort, ControlCommand.CMD_CONTROL_PAUSE);
    			mPlayPauseButton.setBackgroundResource(R.drawable.remote_player_control_play);
    			break;
    		}
    	}
    };
    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remote);
		mVibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
        mGestureDetector = new GestureDetector(this, new TouchListener());
        initControlView();
        getData();
    }
    
    private void getData(){
    	Intent intent = getIntent();
    	if (intent != null && getIntent().getCharSequenceExtra("ip_hostname") != null){
    		String ip_hostname = intent.getCharSequenceExtra("ip_hostname").toString();
        	String[] ip_hostname_arr = ip_hostname.split("-");
        	mRemoteIp = ip_hostname_arr[0];
        	mRemotePort = Utils.COMMAND_PORT;
        	mRemoteHostname = ip_hostname_arr[1];
        	mRemoteComputer = new ComputerInfo(mRemoteIp, mRemoteHostname);
    	}
    }
    
    private void initControlView(){
    	mMessageTextView = (TextView) findViewById(R.id.text_control_message);
    	for (int i = 0; i < mControlButtons.length; i ++){
    		Button controlButton = (Button) findViewById(mControlButtons[i]);
    		controlButton.setOnClickListener(this);
    	}
    	mTouchView = (ImageView) findViewById(R.id.imageViewTouchBackground);
        //mTouchView.setOnClickListener(this);
        mTouchView.setOnTouchListener(this);
        mPlayPauseButton = (Button) findViewById(R.id.button_player_play_pause);
    }
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.button_player_next:
			mHandler.sendEmptyMessage(REMOTE_PLAYER_NEXT);
			break;
		case R.id.button_player_play_pause:
			if (isPlay){
				mHandler.sendEmptyMessage(REMOTE_PLAYER_PLAY);
				
			} else {
				mHandler.sendEmptyMessage(REMOTE_PLAYER_PAUSE);
			}
			isPlay = !isPlay;
			break;
		case R.id.button_player_pre:
			mHandler.sendEmptyMessage(REMOTE_PLAYER_PRE);

			break;
		}
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if (mGestureDetector.onTouchEvent(event)){
			return true;
		} else {
			return false;
		}
	}
	
	public class TouchListener extends GestureDetector.SimpleOnGestureListener {
		@Override
		public boolean onSingleTapUp(MotionEvent ev) { 
			mVibrator.vibrate(50);
		    return true; 
		} 
		@Override
		public void onShowPress(MotionEvent ev) { 
		} 
		@Override
		public void onLongPress(MotionEvent ev) { 
		} 
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
		    return true; 
		} 
		@Override
		public boolean onDown(MotionEvent ev) { 
		    return true; 
		} 
		
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
		     double deltX = Math.abs(e1.getX() - e2.getX());
		     double deltY = Math.abs(e1.getY() - e2.getY());
		     
		     if (deltX >= deltY){
			     if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
		                 && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
			    	 mHandler.sendEmptyMessage(TOUCH_FOCUS_LEFT);
		         } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
		                 && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
		        	 mHandler.sendEmptyMessage(TOUCH_FOCUS_RIGHT);
		         }
		     } else {
		    	 if (e1.getY() - e2.getY() > FLING_MIN_DISTANCE
		        		 && Math.abs(velocityY) > FLING_MIN_VELOCITY){
		    		 mHandler.sendEmptyMessage(TOUCH_FOCUS_UP);
		         } else if (e2.getY() - e1.getY() > FLING_MIN_DISTANCE
		        		 && Math.abs(velocityY) > FLING_MIN_VELOCITY){
		        	 mHandler.sendEmptyMessage(TOUCH_FOCUS_DOWN);
		         }
		     }
		     
		     mVibrator.vibrate(50); 
		     return true; 
		} 
	}
}
